<?php
/**
 * Created by PhpStorm.
 * User: MR_THONG
 * Date: 3/1/2015
 * Time: 8:57 AM
 */

App::uses('AppController', 'Controller');

class UsersController extends AppController {
    function admin_dashboard() {
        $this->set('title_for_layout', 'Welcome to Control Panel');
    }
    function admin_login_form() {
        // save user vao ss
        $user = $this->Session->read('User');
        if (!empty($user)) {
            $this->redirect(array('action' => 'admin_index'));
        }

        if (!empty($this->data['User']['username'])) {
            $us = $this->data['User']['username'];
            $pw = $this->data['User']['password'];
            $condition = array('conditions' => array('User.username' => $us, 'User.password' => $pw));
            $user = $this->User->find('all', $condition);

//            $user = $this->User->find('all', array('conditions' => array('User.username' => $this->data['User']['username'], 'User.password' =>($this->data['User']['password']))));
            if($user) {
                $this->Session->setFlash('Thank you for logging in!');
                $this->Session->write('User', $user);
                $this->Redirect(array('controller' => '', 'action' => 'index'));
                exit();
            } else {
                $this->Session->setFlash('Incorrect username/password!');
                $this->Redirect(array('action' => 'login_form'));
                exit();
            }
        }
    }
    function admin_index() {
        $this->set('title_for_layout', 'Quản lý thành viên');
        $this->set('datas', $this->paginate());
        $data = $this->User->find('all');
        $this->set('listUser',$data);
    }
    function admin_logout() {

        $this -> Session -> destroy();
        $this -> Session -> setFlash('You have been logged out!');

        $this -> Redirect(array('action' => 'login_form'));
        exit();
    }
}